package com.reactlibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.IPackageDeleteObserver;
import android.content.pm.IPackageInstallObserver;
import android.device.DeviceManager;
import android.device.ScanManager;
import android.device.scanner.configuration.Triggering;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.ubx.scanner.StatusListener;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.bean.CustomRegionBean;
import com.ubx.usdk.bean.RfidParameter;
import com.ubx.usdk.bean.Tag6B;
import com.ubx.usdk.bean.Tag6C;
import com.ubx.usdk.listener.InitListener;
import com.ubx.usdk.rfid.aidl.IRfidCallback;
import com.ubx.usdk.rfid.aidl.RfidDate;
import com.ubx.usdk.util.LogUtils;
import com.ubx.usdk.rfid.RfidManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class PdaRFIDModule extends ReactContextBaseJavaModule implements IRfidCallback, InitListener {
    private final String TAG = "PdaRFIDModule";
    private final ReactApplicationContext reactContext;
    private boolean enableLog = true;
    private static final String YBX_SCAN_ACTION = ScanManager.ACTION_DECODE;
    private static RfidManager rfidManager;

    private final BroadcastReceiver scanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                WritableMap params = Arguments.createMap();
                String actionName = intent.getAction();
                if (YBX_SCAN_ACTION.equals(actionName)) {
                    String code = intent.getStringExtra(ScanManager.BARCODE_STRING_TAG);
                    params.putString("code", code);
                    logPrint("code : " + code);
                }
                sendEvent("onEvent", params);
            } catch (Exception e) {
                WritableMap errorParams = Arguments.createMap();
                errorParams.putString("message", e.getMessage());
                sendEvent("onError", errorParams);
            }
        }
    };

    private void sendEvent(String eventName, WritableMap params) {
        getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
                params);
    }

    public PdaRFIDModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;

    }

    private Callback callbackInit;
    @ReactMethod
    public void power(boolean enable) {
        int ret = RFIDSDKManager.getInstance().power(enable);
        logPrint("power()  " + enable + "   " + ret);
    }

    @ReactMethod
    public void powerAsync(boolean enable, Promise promise) {
        try {
            int ret = RFIDSDKManager.getInstance().power(enable);
            logPrint("power()  " + enable + "   " + ret);
            promise.resolve(ret); // 返回结果
        } catch (Exception e) {
            promise.reject("POWER_ERROR", e); // 处理错误
        }
    }

    // @ReactMethod
    // public boolean connect() {
    // boolean con = RFIDSDKManager.getInstance().connect();
    // logPrint("connect()" +con);
    // return con;
    // }

    @ReactMethod
    public void connect(Promise promise) {
        try {
            boolean con = RFIDSDKManager.getInstance().connect();
            logPrint("connect()" + con);
            promise.resolve(con); // 返回结果
        } catch (Exception e) {
            promise.reject("CONNECT_ERROR", e); // 处理错误
        }
    }

    // @ReactMethod
    // public boolean disConnect() {
    // boolean discon = RFIDSDKManager.getInstance().disConnect();
    // logPrint("disConnect() " +discon);
    // return discon;
    // }

    @ReactMethod
    public void disConnect(Promise promise) {
        try {
            boolean discon = RFIDSDKManager.getInstance().disConnect();
            logPrint("connect()" + discon);
            promise.resolve(discon); // 返回结果
        } catch (Exception e) {
            promise.reject("CONNECT_ERROR", e); // 处理错误
        }
    }

    @ReactMethod
    public void init(Callback callback) {
        callbackInit = callback;
        logPrint("init()");
        RFIDSDKManager.getInstance().init(this);
    }

    @Override
    public void onStatus(boolean b) {
        logPrint("onStatus()  " + b);
        if (b) {
          rfidManager = RFIDSDKManager.getInstance().getRfidManager();
        }else {
        }
        if (callbackInit != null) {
            callbackInit.invoke(b);
        }
    }

    @Override
    public void onInventoryTag(String epc, String data, String rssi) {
        WritableMap params = Arguments.createMap();
        params.putString("epc", epc);
        params.putString("data", data);
        params.putString("rssi", rssi);
        sendEvent("onInventoryTag", params);
    }

    @Override
    public void onInventoryTagEnd() {
        WritableMap params = Arguments.createMap();
        sendEvent("onInventoryTagEnd", params);
    }

    @ReactMethod
    public void registerCallback() {
        Log.i(TAG, "registerCallback()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().registerCallback(this);
        } else {
            logPrint("registerCallback()   getRfidManager() == null");
        }
    }


    @ReactMethod
    public void unregisterCallback() {
        Log.i(TAG, "unregisterCallback()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().unregisterCallback(this);
        } else {
            logPrint("unregisterCallback()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void isRfidInitialized(Promise promise) {
        boolean isInitialized = rfidManager != null;
        if (!isInitialized) {
            logPrint("isRfidInitialized()   getRfidManager() == null");
        }
        logPrint("isRfidInitialized()  " + isInitialized + " " + RFIDSDKManager.getInstance().getRfidManager());
        promise.resolve(isInitialized); // 通过 Promise 返回结果
    }

    // @ReactMethod
    // public void isConnected(Callback callback){
    // Log.i(TAG,"isConnected()");
    // boolean isConnect = false;
    // if (RFIDSDKManager.getInstance().getRfidManager() !=null){
    // isConnect = RFIDSDKManager.getInstance().getRfidManager().isConnected();
    // }else {
    // logPrint( "isConnected() getRfidManager() == null");
    // }
    // if (callback!=null){
    // callback.invoke(isConnect);
    // }
    // }
    @ReactMethod
    public void isConnected(Promise promise) {
        Log.i(TAG, "isConnected()");
        boolean isConnect = false;
        try {
            if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                isConnect = RFIDSDKManager.getInstance().getRfidManager().isConnected();
            } else {
                logPrint("isConnected()   getRfidManager() == null");
            }
            promise.resolve(isConnect); // 返回连接状态
        } catch (Exception e) {
            promise.reject("IS_CONNECTED_ERROR", e); // 处理错误
        }
    }

    @ReactMethod
    public void sendCommand(byte[] btCMDPackage, Callback callback) {
        int ret = -1;
        Log.i(TAG, "sendCommand()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().sendCommand(btCMDPackage);
        } else {
            logPrint("sendCommand()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void startInventory(int btSession, Callback callback) {
        int ret = -1;
        Log.i(TAG, "startInventory()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startInventory(btSession);
        } else {
            logPrint("startInventory()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void stopInventory(Callback callback) {
        int ret = -1;
        Log.i(TAG, "stopInventory()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().stopInventory();
        } else {
            logPrint("stopInventory()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getFirmwareVersion(Callback callback) {
        String ret = "";
        Log.i(TAG, "getFirmwareVersion()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getFirmwareVersion();
        } else {
            logPrint("getFirmwareVersion()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getFrequencyRegion(Callback callback) {
        int[] aar = new int[4];
        Log.i(TAG, "getFrequencyRegion()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RfidDate rfidDate = RFIDSDKManager.getInstance().getRfidManager().getFrequencyRegion();
            if (rfidDate != null) {
                aar[0] = byteToUnsignedInt(rfidDate.btWorkAntenna);
                aar[1] = byteToUnsignedInt(rfidDate.btRegion);
                aar[2] = byteToUnsignedInt(rfidDate.btFrequencyStart);
                aar[3] = byteToUnsignedInt(rfidDate.btFrequencyEnd);

                if (callback != null) {
                    WritableArray writableArray = Arguments.createArray();
                    for (int i : aar) {
                        writableArray.pushInt(i);
                    }
                    callback.invoke(writableArray);
                    return;
                }

            }
        } else {
            logPrint("getFrequencyRegion()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke();
        }
    }

    public int byteToUnsignedInt(byte b) {
        return b & 0xFF;
    }

    @ReactMethod
    public void getOutputPower(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getOutputPower()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getOutputPower();
        } else {
            logPrint("getOutputPower()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getReaderTemperature(Callback callback) {
        String ret = "";
        Log.i(TAG, "getReaderTemperature()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getReaderTemperature();
        } else {
            logPrint("getReaderTemperature()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void killTag(String epc, String btAryPassWord, Callback callback) {
        int ret = -1;
        Log.i(TAG, "killTag()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().killTag(epc, btAryPassWord);
        } else {
            logPrint("killTag()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void lockTag(String epc, String btAryPassWord, int btMemBank, int btLockType, Callback callback) {
        int ret = -1;
        Log.i(TAG, "lockTag()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().lockTag(epc, btAryPassWord, btMemBank, btLockType);
        } else {
            logPrint("lockTag()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    // @ReactMethod
    // public void readTag(String epc , int btMemBank, int btWordAdd, int btWordCnt,
    // String btAryPassWord, Callback callback){
    // String ret = "";
    // if (RFIDSDKManager.getInstance().getRfidManager() !=null){
    // ret = RFIDSDKManager.getInstance().getRfidManager().readTag(epc, btMemBank,
    // btWordAdd, btWordCnt, btAryPassWord);
    // }else {
    // logPrint( "readTag() getRfidManager() == null");
    // }
    // if (callback!=null) {
    // callback.invoke(ret);
    // }
    // Log.i(TAG,"readTag() "+ret);
    // }

    @ReactMethod
    public void readTag(String epc, int btMemBank, int btWordAdd, int btWordCnt, String btAryPassWord,
            Promise promise) {
        String ret = "";
        try {
            if (RFIDSDKManager.getInstance().getRfidManager() != null) {
                ret = RFIDSDKManager.getInstance().getRfidManager().readTag(epc, btMemBank, btWordAdd, btWordCnt,
                        btAryPassWord);
            } else {
                logPrint("readTag()   getRfidManager() == null");
                promise.reject("READ_TAG_ERROR", "getRfidManager() == null"); // 处理错误
                return; // 退出方法
            }
            Log.i(TAG, "readTag()  " + ret);
            promise.resolve(ret); // 返回结果
        } catch (Exception e) {
            promise.reject("READ_TAG_EXCEPTION", e); // 处理异常
        }
    }

    @ReactMethod
    public void setFrequencyRegion(int btRegion, int btStartRegion, int btEndRegion, Callback callback) {
        int ret = -1;

        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setFrequencyRegion(btRegion, btStartRegion,
                    btEndRegion);
        } else {
            logPrint("setFrequencyRegion()   getRfidManager() == null");
        }
        Log.i(TAG, "setFrequencyRegion()  ret == " + ret);
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setOutputPower(int btOutputPower, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setOutputPower()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setOutputPower(btOutputPower);
        } else {
            logPrint("setOutputPower()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeTag(String epc, String btAryPassWord, int btMemBank, int btWordAdd, String btAryData,
            Callback callback) {
        int ret = -1;

        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeTag(epc, btAryPassWord, btMemBank, btWordAdd,
                    btAryData);
        } else {
            logPrint("writeTag()   getRfidManager() == null");
        }

        Log.i(TAG, "writeTag()  ret == " + ret);
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getModule(Callback callback) {
        String ret = "";
        Log.i(TAG, "getModule()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getModule();
        } else {
            logPrint("getModule()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getModuleFirmware(Callback callback) {
        String ret = "";
        Log.i(TAG, "getModuleFirmware()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getModuleFirmware();
        } else {
            logPrint("getModuleFirmware()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeEpcString(String epc, String password, Callback callback) {
        int ret = -1;
        Log.i(TAG, "writeEpcString()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeEpcString(epc, password);
        } else {
            logPrint("writeEpcString()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeEpc(int epcLen, String epc, String btAryPassWord, Callback callback) {
        int ret = -1;
        Log.i(TAG, "writeEpc()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeEpc(epcLen, epc, btAryPassWord);
        } else {
            logPrint("writeEpc()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeTagByTid(String TIDStr, int Mem, int WordPtr, String Password, String wdata, Callback callback) {
        int ret = -1;
        Log.i(TAG, "writeTagByTid()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeTagByTid(TIDStr, Mem, WordPtr, Password, wdata);
        } else {
            logPrint("writeTagByTid()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setScanInterval(int interval, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setScanInterval()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setScanInterval(interval);
        } else {
            logPrint("setScanInterval()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getScanInterval(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getScanInterval()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getScanInterval();
        } else {
            logPrint("getScanInterval()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setQueryMode(int mode) {
        Log.i(TAG, "setQueryMode()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().setQueryMode(mode);
        } else {
            logPrint("setQueryMode()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void getQueryMode(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getQueryMode()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getQueryMode();
        } else {
            logPrint("getQueryMode()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getReaderType(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getReaderType()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getReaderType();
        } else {
            logPrint("getReaderType()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void startRead(Callback callback) {
        int ret = -1;
        Log.i(TAG, "startRead()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startRead();
        } else {
            logPrint("startRead()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void scanRfid() {
        Log.i(TAG, "scanRfid()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().scanRfid();
        } else {
            logPrint("scanRfid()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void setInventoryParameter(ReadableArray arrayKey) {

        Log.i(TAG, "setInventoryParameter()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {

            int[] parArr = new int[arrayKey.size()];

            for (int i = 0; i < arrayKey.size(); i++) {
                parArr[i] = arrayKey.getInt(i);
            }
            RfidParameter parameter = RFIDSDKManager.getInstance().getRfidManager().getInventoryParameter();
            parameter.IvtType = parArr[0];
            parameter.Memory = parArr[1];
            parameter.WordPtr = parArr[2];
            parameter.Length = parArr[3];
            parameter.Session = parArr[4];
            parameter.QValue = parArr[5];
            parameter.ScanTime = parArr[6];
            parameter.Interval = parArr[7];
            RFIDSDKManager.getInstance().getRfidManager().setInventoryParameter(parameter);
        } else {
            logPrint("setInventoryParameter()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void getInventoryParameter(Callback callback) {
        int[] parArr = new int[8];
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RfidParameter parameter = RFIDSDKManager.getInstance().getRfidManager().getInventoryParameter();
            parArr[0] = parameter.IvtType;
            parArr[1] = parameter.Memory;
            parArr[2] = parameter.WordPtr;
            parArr[3] = parameter.Length;
            parArr[4] = parameter.Session;
            parArr[5] = parameter.QValue;
            parArr[6] = parameter.ScanTime;
            parArr[7] = parameter.Interval;

            if (callback != null) {
                WritableArray writableArray = Arguments.createArray();
                for (int i : parArr) {
                    writableArray.pushInt(i);
                }
                callback.invoke(writableArray);
            }
        } else {
            logPrint("setInventoryParameter()   getRfidManager() == null");
            if (callback != null) {
                callback.invoke();
            }
        }
    }

    @ReactMethod
    public void getDeviceId(Callback callback) {
        String ret = "";
        Log.i(TAG, "getDeviceId()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getDeviceId();
        } else {
            logPrint("getDeviceId()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    /** 设置自定义频段 */
    @ReactMethod
    public void setCustomRegion(int flags, int band, int FreSpace, int FreNum, int StartFre, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setCustomRegion()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setCustomRegion(flags, FreSpace, FreNum, StartFre);
        } else {
            logPrint("setCustomRegion()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    /** 获取自定义频段参数,不要用获取设置普通频段的方法来获取参数。 */
    @ReactMethod
    public void getCustomRegion(Callback callback) {
        int[] arr = new int[4];
        Log.i(TAG, "getCustomRegion()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            CustomRegionBean customRegion = RFIDSDKManager.getInstance().getRfidManager().getCustomRegion();
            arr[0] = customRegion.band[0];
            arr[1] = customRegion.FreSpace[0];
            arr[2] = customRegion.FreSpace[0];
            arr[3] = customRegion.StartFre[0];

            if (callback != null) {
                WritableArray writableArray = Arguments.createArray();
                for (int i : arr) {
                    writableArray.pushInt(i);
                }
                callback.invoke(writableArray);
            }
        } else {
            logPrint("getCustomRegion()   getRfidManager() == null");
            if (callback != null) {
                callback.invoke();
            }
        }
    }

    /** 获取配置信息 */
    @ReactMethod
    public void getProfile(Callback callback) {
        byte[] arr = null;
        Log.i(TAG, "getProfile()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            arr = RFIDSDKManager.getInstance().getRfidManager().getProfile();
            if (callback != null) {
                WritableArray writableArray = Arguments.createArray();
                for (byte i : arr) {
                    writableArray.pushInt((int) (arr[i] & 0xFF)); // 转换成无符号字节形式
                }
                callback.invoke(writableArray);
            }
        } else {
            logPrint("getProfile()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void setProfile(int param, Callback callback) {
        int ret = -1;
        Log.i(TAG, "startRead()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startRead();
        } else {
            logPrint("startRead()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void addMask(int mem, int startAdd, int length, String data, Callback callback) {
        int ret = -1;
        Log.i(TAG, "startRead()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startRead();
        } else {
            logPrint("startRead()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void clearMask() {
        int ret = -1;
        Log.i(TAG, "clearMask()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startRead();
        } else {
            logPrint("clearMask()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void readDataByTid(String TIDStr, int Mem, int WordPtr, int Num, String Password, Callback callback) {
        String ret = "";
        Log.i(TAG, "readDataByTid()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().readDataByTid(TIDStr, Mem, WordPtr, Num, Password);
        } else {
            logPrint("readDataByTid()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setFocus(int mode, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setFocus()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setFocus(mode);
        } else {
            logPrint("setFocus()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getFocus(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getFocus()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getFocus();
        } else {
            logPrint("getFocus()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setRange(int range, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setRange()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setRange(range);
        } else {
            logPrint("setRange()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getRange(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getRange()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getRange();
        } else {
            logPrint("getRange()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void killTagByTid(int tidlen, String tid, String Password, Callback callback) {
        int ret = -1;
        Log.i(TAG, "killTagByTid()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().killTagByTid(tidlen, tid, Password);
        } else {
            logPrint("killTagByTid()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void lockbyTID(int tidlen, String tid, int select, int setprotect, String Password, Callback callback) {
        int ret = -1;
        Log.i(TAG, "lockbyTID()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().lockbyTID(tidlen, tid, select, setprotect, Password);
        } else {
            logPrint("lockbyTID()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void inventorySingle(Callback callback) {
        int ret = -1;
        Log.i(TAG, "inventorySingle()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().inventorySingle();
        } else {
            logPrint("inventorySingle()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void setDwellTime(int key, Callback callback) {
        int ret = -1;
        Log.i(TAG, "setDwellTime()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().setDwellTime(key);
        } else {
            logPrint("setDwellTime()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void getDwellTime(Callback callback) {
        int ret = -1;
        Log.i(TAG, "getDwellTime()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().getDwellTime();
        } else {
            logPrint("getDwellTime()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void startInventoryLed(int factory, List<String> epcs, Callback callback) {
        int ret = -1;
        Log.i(TAG, "startInventoryLed()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().startInventoryLed(factory, epcs);
        } else {
            logPrint("startInventoryLed()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void stopInventoryLed() {
        Log.i(TAG, "stopInventoryLed()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().stopInventoryLed();
        } else {
            logPrint("stopInventoryLed()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void enableLog(Boolean enable) {
        Log.i(TAG, "enableLog()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            RFIDSDKManager.getInstance().getRfidManager().enableLog(enable);
        } else {
            logPrint("enableLog()   getRfidManager() == null");
        }
    }

    @ReactMethod
    public void inventoryOnce(int session, int qvalue, int tidAddr, int tidLen, int target, int scantime,
            Callback callback) {
        int ret = -1;
        Log.i(TAG, "inventoryOnce()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().inventoryOnce(session, qvalue, tidAddr, tidLen, target,
                    scantime);
        } else {
            logPrint("inventoryOnce()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void inventoryOnceMix(int session, int qvalue, int target, int scantime, int readMem, byte[] ReadAdr,
            int ReadLen, String Pwd, Callback callback) {
        int ret = -1;
        Log.i(TAG, "inventoryOnceMix()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().inventoryOnceMix(session, qvalue, target, scantime,
                    readMem, ReadAdr, ReadLen, Pwd);
        } else {
            logPrint("inventoryOnceMix()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void maskReadTag(int Mem, int WordPtr, int Num, String Password, int mem, int startAdd, int length,
            String datasMask, Callback callback) {
        String ret = "";
        Log.i(TAG, "maskReadTag()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().maskReadTag(Mem, WordPtr, Num, Password, mem, startAdd,
                    length, datasMask);
        } else {
            logPrint("maskReadTag()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void maskWriteTag(int WNum, String strData, int Mem, int WordPtr, int Num, String Password, int mem,
            int startAdd, int length, String datasMask, Callback callback) {
        int ret = -1;
        Log.i(TAG, "maskWriteTag()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().maskWriteTag(WNum, strData, Mem, WordPtr, Num, Password,
                    mem, startAdd, length, datasMask);
        } else {
            logPrint("maskWriteTag()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void lightUpLedTag(String epc, String pwd, int time, Callback callback) {
        int ret = -1;
        Log.i(TAG, "lightUpLedTag()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().lightUpLedTag(epc, pwd, time);
        } else {
            logPrint("lightUpLedTag()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void enableRF(boolean on, Callback callback) {
        int ret = -1;
        Log.i(TAG, "enableRF()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().enableRF(on);
        } else {
            logPrint("enableRF()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeTagEpc(String selectEpc, String password, String writeData, Callback callback) {

        int ret = -1;
        Log.i(TAG, "writeTagEpc()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeTagEpc(selectEpc, password, writeData);
        } else {
            logPrint("writeTagEpc()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void writeTagEpcByTid(String TIDStr, String password, String writeData, Callback callback) {
        int ret = -1;
        Log.i(TAG, "writeTagEpcByTid()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            ret = RFIDSDKManager.getInstance().getRfidManager().writeTagEpcByTid(TIDStr, password, writeData);
        } else {
            logPrint("writeTagEpcByTid()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke(ret);
        }
    }

    @ReactMethod
    public void findEpc(String epc, Callback callback) {
        String[] tags = new String[3];
        Log.i(TAG, "findEpc()");
        if (RFIDSDKManager.getInstance().getRfidManager() != null) {
            Tag6C tag6C = RFIDSDKManager.getInstance().getRfidManager().findEpc(epc);
            if (tag6C != null) {
                tags[0] = tag6C.epcId;
                tags[1] = tag6C.memId;
                tags[2] = String.valueOf(tag6C.rssi);
                if (callback != null) {
                    WritableArray writableArray = Arguments.createArray();
                    for (String i : tags) {
                        writableArray.pushString(i);
                    }
                    callback.invoke(writableArray);
                    return;
                }
            }
        } else {
            logPrint("findEpc()   getRfidManager() == null");
        }
        if (callback != null) {
            callback.invoke();
        }
    }

    @ReactMethod
    public void enableScanHead(boolean isOpen) {
        RFIDSDKManager.getInstance().enableScanHead(isOpen);
    }

    @Override
    public String getName() {
        return "PdaRFIDModule";
    }

    @Override
    public boolean canOverrideExistingModule() {
        return true;
    }

    private void logPrint(String content) {
        if (enableLog) {
            Log.i(TAG, content);
        }
    }

}
